<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Admin
 *
 * @author root
 */
class Auth_Controller_Admin_Default extends MVC_AbstractController
{
    private $authModel;
    
    public function __construct()
    {
        parent::__construct();

        Config::set('app.template', SRC_DIR . DS . 'Templates/Admin/login.php');
        
        $this->authModel = new Auth_Model_Admin_Auth();
    }
    
    public static function checkLogin()
    {
        return fSession::get('admin', FALSE);
    }
    
	public function doLogout()
	{
		fSession::remove('admin');
		
		$this->redirect($this->getLink('Auth', 'Default', 'login'));	
	}
	
    public function doLogin()
    {

        $checkAuth = $this->authModel->checkLogin(fRequest::get('username'), fRequest::get('password'));
        
        if($checkAuth)
        {
            fSession::set('admin',$checkAuth);
            
			$this->logUserAccess($checkAuth);
			
            $this->redirect($this->getLink('Dashboard', 'Default', 'index'));
        }
        else
        {
		
            fMessaging::create('loginError', 'login', Util::message('User not found or username password not match', 'error'));
            
            $this->redirect($this->getLink('Auth', 'Default', 'login'));
        }
    }
    
	private function logUserAccess($userRow = array())
	{
		$geoInfo 					  = Util::geoByIp();
		
		$insertArray['user_id'] 	  = $userRow['id'];

        $insertArray['login_at']	  = date('Y-m-d : H:i:s');

        $insertArray['ip']      	  = Util::getRealIpAddr();

        $insertArray['user_id'] 	  = $userRow['id'];

        $insertArray['address_by_ip'] = $geoInfo['country_name'].' '.$geoInfo['country_code'].' '.$geoInfo['city'];
        
        $insertArray['browser_info']  = Util::getBrowser(); 

        $insertArray['os_info']       = Util::getOs(); 

        $insertArray['session_id']    = session_id();
	
		return $this->authModel->db->create('user_login_log', $insertArray);

	}
	
    public function login()
    {
        
        $html = '<div class="row-fluid">
            
                        <div class="well span5 center login-box">
                        
                            <div class="alert alert-info">
                            
				Please login with your Username and Password.
                            
                            </div>
                            
                            <form class="form-horizontal" action="'.Config::get('app.router')->getLink('Auth', 'Default', 'doLogin').'" method="post" auto-complete="false">
                            
                                <fieldset>
                                
                                    <div class="input-prepend" title="Username" data-rel="tooltip">
				
                                        <span class="add-on"><i class="icon-user"></i></span><input autofocus class="input-large span10" name="username" id="username" type="text" />
                                    
                                    </div>
                                    
                                    <div class="clearfix"></div>

                                    <div class="input-prepend" title="Password" data-rel="tooltip">
                                    
                                        <span class="add-on"><i class="icon-lock"></i></span><input class="input-large span10" name="password" id="password" type="password" />
                                    
                                    </div>
                                    
                                    <div class="clearfix"></div>

                                    <div class="input-prepend">
                                    
                                        <label class="remember" for="remember"><input type="checkbox" id="remember" />Remember me</label>
                                    
                                    </div>
                                    
                                    <div class="clearfix"></div>

                                    <p class="center span5">
                                        
                                        <button type="submit" class="btn btn-primary">Login</button>
                                    
                                    </p>
                                
                                </fieldset>
                            
                            </form>
                        
                        </div>
                
                </div>';

        return $html;

    }

}